package sample;

import java.util.LinkedList;
import javafx.scene.control.TextArea;
public class SPN {
    //最短进程优先shortest process next
    public SPN(LinkedList<PCB> pcblist,TextArea txtprocess){
        int min = 0,min_time=0,i;
        while (!pcblist.isEmpty()){
            for (int j = 0; j < pcblist.size(); j++) {
                min=0;
                min_time=pcblist.get(0).getTime();
                if (pcblist.get(j).getTime() < min_time && pcblist.get(j).getStatus() == 0) {
                    min=j;
                    min_time=pcblist.get(j).getTime();
                }
            }
            for (int j = min_time; j > 0; j--) {
                pcblist.get(min).setTime(j-1);//非抢占
                if (pcblist.get(min).getTime()<=0)
                pcblist.get(min).setStatus(-1);//置为终止态
                for ( i = 0; i < pcblist.size(); i++) {
                    txtprocess.appendText(pcblist.get(i).print_pcb());
                }
                txtprocess.appendText("-------------------------------------------------------------------------------------------\n");
            }

            if (pcblist.get(min).getStatus()==-1)
                pcblist.remove(min);

        }
    }
}
